package com.qf.springboot3.controller;

import com.qf.springboot3.domain.Stu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/stu")
//启用配置属性对象，将指定类型的对象添加到spring容器中，并且将application.yml中对应的属性值封装到该对象
@EnableConfigurationProperties(Stu.class)
public class StudentController {

    @Value("${stu.name}")
    private String name;

    @Value("${stu.age}")
    private Integer age;

    //这种方式读取数据无法读取一个完整内容，只能挨个读取
    @Value("${stu.hobbies[0]}")
    private String hobby1;
    @Value("${stu.hobbies2[1]}")
    private String hobby2;

    //当springboot项目启动时，会将application.yml文件中的所有属性值都封装到这个对象
    @Autowired
    private Environment env;


    @Autowired
    private Stu stu;

    @GetMapping("/test1")
    public void test1(){
        System.out.println("name:" + name);
        System.out.println("age:" + age);
        System.out.println("hobby1:" + hobby1);
        System.out.println("hobby2:" + hobby2);
    }

    @GetMapping("/test2")
    public void test2(){
        String name = env.getProperty("stu.name");
        Integer age = env.getProperty("stu.age", Integer.class);

        //这种方式读取数据无法读取一个完整内容，只能挨个读取
        String hobby1 = env.getProperty("stu.hobbies[2]");

        System.out.println("name:" + name);
        System.out.println("age:" + age);
        System.out.println("hobby1:" + hobby1);
    }

    @GetMapping("/test3")
    public void test3(){
        System.out.println(stu);
    }
}
